import React, { Component } from 'react'

export default class User extends Component {

  // state第一个参数: 函数写法、对象写法
  // state第二个参数: 处理异步情况 获取最新count

  state = {
    count : 1
  }

  test1 = () => {
    this.setState(state => ({count: this.state.count + 1}))
  }

  test2 = () => {
    const count = this.state.count + 1
    this.setState ({
      count
    })
  }

  test3 = () => {
    this.setState(state => ({count: this.state.count + 1}), () => {
      console.log ('第二个参数获取最新count', this.state.count)
    })
  }
  render() {
    console.log ('A render()')
    return (
      <div>
        <h1>A组件: {this.state.count}</h1>
        <button onClick={this.test1}>A 测试1</button> &nbsp; &nbsp;
        <button onClick={this.test2}>A 测试2</button> &nbsp; &nbsp;
        <button onClick={this.test3}>A 测试3</button> &nbsp; &nbsp;
      </div>
    )
  }
}
